﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace TEAA
{
    public class Projection : RelationalOperation
    {
        private IDictionary<string, string> attributes;

        public Projection(IDictionary<string, string> attributes)
        {
            this.attributes = attributes;
        }

        public DataSet Execute(DataSet data)
        {
            DataSet projection = data.Copy();
            System.Diagnostics.Trace.WriteLine("proj " + projection.Tables[0].Columns[1].ColumnName);

            foreach (DataTable table in projection.Tables)
            {
                List<DataColumn> toRemove = new List<DataColumn>();
                DataColumnCollection dataColumns = table.Columns;

                foreach (DataColumn column in dataColumns)
                {
                    if (!attributes.ContainsKey(column.ColumnName))
                        toRemove.Add(column);
                    else
                    {
                        if (projection.Tables.Contains(attributes[column.ColumnName]) && !table.TableName.Equals(attributes[column.ColumnName]))
                            toRemove.Add(column);
                    }
                }

                foreach (DataColumn column in toRemove)
                    dataColumns.Remove(column);
            }

            System.Diagnostics.Trace.WriteLine("proj " + projection.Tables[0].Columns[0].ColumnName);

            return projection;
        }
    }
}
